// pages/comment/comment.js
import Toast from '../../lib/vant-weapp/toast/toast';
const jeegot = require('../../utils/jeegot.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    page: 1,
    count: {
    },
    loaded: false,
    goodCommentRate: 100,
    rate: 5,
    comment: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    // this.setData({
    //   comment: this.data.commentList
    // })

    let productId = options.productId
    this.setData({productId: productId})

    this.getCommentStatCount()
    this.getCommentList()

  },

  getCommentStatCount: function(){
    jeegot.getProductCommentStatCount({productId: this.data.productId}).then(data =>{
      this.setData({count: data})

      let score = this.data.count.goodCommentRate
      let rate = 5;
      if(score>=90) {
        rate = 5
      } else if(score >=60) {
        rate = 4
      } else if(score >=50) {
        rate = 3
      } else if(score >=30) {
        rate = 2
      } else {
        rate = 1
      }
      this.setData({rate : rate})
    })
    
  },

  getCommentList: function(){
    let me = this
    jeegot.pageProductCommentByProductId(
      {productId: me.data.productId, type: me.data.active, page:me.data.page}
    ).then(data =>{
      me.setData({comment:data.records})
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeTag: function (e) {
    let tag = e.currentTarget.dataset.value;
    this.setData({
      page: 1,
      active: tag
    });
   
    this.getCommentList();
    
  },

  scrollListen: function (e) {
    let me = this
    if(this.data.page<=this.data.totalPage) {
      let cpage = me.data.page;
      me.setData({page: cpage + 1})
      me.getCommentList()
    } else {
      // Toast("没有更多评论了")
      this.setData({loaded: true})
    }
  }

})